﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using QLDV.Models;
using QLDV.ViewModels;

namespace QLDV.Controllers
{
    public class CsdController : BaseController
    {
        //
        // GET: /Csd/

        public ActionResult Index()
        {
            return View(Repository.GetAllCsd());
        }

        //
        // GET: /Csd/Details/5

        public ActionResult Details(int id)
        {
            return View();
        }

        //
        // GET: /Csd/Create

        [HttpGet]
        public ActionResult Create()
        {
            CSDModel csd = new CSDModel();
            List<LOAICSD> capQL = Repository.GetAllLoaiCSD();
            List<SelectListItem> csdQl = new List<SelectListItem>();
            List<CSD> csdList = Repository.GetAllCsd();
            int id_LoaiCSDQL = 1;
            if((capQL.Count !=0) && (csdList.Count != 0))
            {
                id_LoaiCSDQL = (int)capQL.First().Id_LoaiCSD;
                capQL.RemoveAt(0);
            }
            
            List<SelectListItem> resultList = new List<SelectListItem>();
            foreach (var item in capQL)
            {
                resultList.Add(new SelectListItem
                {
                    Value = item.Id_LoaiCSD.ToString(),
                    Text = item.Ten
                }
                );
            }
            ViewData["capQL"] = resultList;
            if (resultList.Count != 0)
            {
                var csdQl_List = Repository.GetByIdLoaiCSD_CSD(id_LoaiCSDQL);
                
                foreach (var item in csdQl_List)
                {
                    csdQl.Add(new SelectListItem
                    {
                        Value = item.Id_CSD.ToString(),
                        Text = item.Ten
                    }
                    );
                }
            }
            ViewData["csdQL"] = csdQl;
            if (Request != null && Request.IsAjaxRequest())
            {
                return PartialView(csd);
            }
            else return RedirectToAction("Index");
        } 

        //
        // POST: /Csd/Create

        [HttpPost]
        public ActionResult Create(CSDModel csd)
        {
            string message = Repository.InsertCSD(csd.returnCSD());
            if (message.Equals("true"))
            {
                return RedirectToAction("Index");
            }
            else
            {
                return RedirectToAction("index","error", message);
            }
        }

        [AcceptVerbs(HttpVerbs.Get)]
        public JsonResult GetListLoaiCsd(int id)
        {
            /*var sbden = this.Repository.LayDSTpDen(id);*/
            //return Json(sbden, JsonRequestBehavior.AllowGet);

            var csdQl_List = Repository.GetByIdLoaiCSD_CSD((int)Repository.GetByIdLoaiCSD(id).Id_LoaiCSDQL);
            List<SelectListItem> csdQl = new List<SelectListItem>();
            foreach (var item in csdQl_List)
            {
                csdQl.Add(new SelectListItem
                {
                    Value = item.Id_CSD.ToString(),
                    Text = item.Ten
                }
                );
            }
            return Json(csdQl, JsonRequestBehavior.AllowGet);
        }
 

    }
}
